﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WPay.Domain.Model.Entities;
using WPay.Infrastructure.Unity;

namespace WPay.Infrastructure.EFCore
{
    public class Entities : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var mysql = ConfigManage.GetConfigByKey("DBConstr");
            optionsBuilder.UseMySql(mysql).EnableDetailedErrors();
        }


        #region 实体引用

        public DbSet<City> City { get; set; }
        public DbSet<Cpn> Cpn { get; set; }
        public DbSet<CpnOrg> CpnOrg { get; set; }
      
        public DbSet<MngrBaseInfo> MngrBaseInfo { get; set; }
        public DbSet<MngrLog> MngrLog { get; set; }
        public DbSet<MngrRole> MngrRole { get; set; }
     
        public DbSet<Province> Province { get; set; }
        public DbSet<SysModule> SysModule { get; set; }
        public DbSet<SysModuleAction> SysModuleAction { get; set; }
        public DbSet<SysPara> SysPara { get; set; }
        public DbSet<SysRole> SysRole { get; set; }
        public DbSet<SysRoleAction> SysRoleAction { get; set; }

        #endregion

        public virtual async Task CommitAsync()
        {
            await SaveChangesAsync();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            #region old code

            //指定实体字段保留小数位数
            //modelBuilder.Entity<Experience>().Property(a => a.dlongitude).HasPrecision(18, 6);
            //modelBuilder.Entity<Experience>().Property(a => a.dlatitude).HasPrecision(18, 6);

            //解除实体与数据库之间的映射
            //modelBuilder.Entity<Project_SYS_Province>().Ignore(p => p.PyProvinceName);
            //modelBuilder.Entity<Project_SYS_City>().Ignore(p => p.PyCityName);

            //系统管理员与管理员角色的多对多关联 主外键关联请手动按需添加
            //modelBuilder.Entity<Project_SYS_ModuleAction>().HasOne(a => a.Module).WithMany(c => c.ModuleActions).HasForeignKey(c => c.ModuleID);  //1:N
            //modelBuilder.Entity<Project_SYS_ModuleAction>().HasOne(o => o.Category).WithOne(o => o.Product);//1:1

            //modelBuilder.Entity<MngrRole>().HasOne(a => a.BaseInfo).WithMany(a => a.Roles).HasForeignKey(a => a.MemberId); //1:N
            //modelBuilder.Entity<SysRoleAction>().HasOne(a => a.Role).WithMany(a => a.RoleActions).HasForeignKey(a => a.RoleID);//1:N

            // modelBuilder.Entity<MngrRole>().HasOne(a => a.BaseInfo).WithMany(a => a.Roles).HasForeignKey(a => a.MemberId); //1:N




            modelBuilder.Entity<CpnOrg>().HasKey(x => new { x.CpnID, x.OrgID });
            modelBuilder.Entity<Cpn>().HasKey(x => new { x.CpnID });
            modelBuilder.Entity<SysModule>().HasKey(x => new { x.ID });
            modelBuilder.Entity<SysModuleAction>().HasKey(x => new { x.ID });
            modelBuilder.Entity<City>().HasKey(x => new { x.ID });
         
            modelBuilder.Entity<MngrBaseInfo>().HasKey(x => new { x.ID });
            modelBuilder.Entity<MngrLog>().HasKey(x => new { x.ID });
            modelBuilder.Entity<MngrRole>().HasKey(x => new { x.ID });
          
            modelBuilder.Entity<MsgRcd>().HasKey(x => new { x.ID });
            modelBuilder.Entity<Province>().HasKey(x => new { x.ID });
            modelBuilder.Entity<SysRole>().HasKey(x => new { x.ID });
            modelBuilder.Entity<SysRoleAction>().HasKey(x => new { x.ID });
            modelBuilder.Entity<WorkflowMo>().HasKey(x => new { x.ID });

            #endregion
        }

        //方便排查异常信息
        public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var result = await base.SaveChangesAsync(cancellationToken);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
